package com.bookmanager.www.common.async;

import cn.hutool.core.util.IdUtil;
import com.bookmanager.www.common.utils.Result;
import com.bookmanager.www.entity.FlPayinfo;
import com.bookmanager.www.mapper.FlBookorderMapper;
import com.bookmanager.www.mapper.FlUserMapper;
import com.bookmanager.www.service.FlPayinfoService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: 风离
 * @Date: 2021/12/02/12:04
 * @Description: 异步处理类 例如消费之后累加积分 累加消费金额 不应该放在主线程中
 */
@Component
@Slf4j
public class AsyncTask {

    @Resource
    private FlUserMapper flUserMapper;

    @Resource
    private FlBookorderMapper flBookorderMapper;

    @Resource
    private FlPayinfoService flPayinfoService;

    public AsyncTask() {
    }

    @SneakyThrows
    @Async
    public void addCumulativeConsumption(String userId, double consumption) {
        log.info("开始累加消费金额");
        Thread.sleep(2000);
        int update = flUserMapper.addCumulativeConsumption(userId,consumption);
        log.info("累计消费金额是否更新？"+update);
        log.info("累加消费金额完成");
    }

    @SneakyThrows
    @Async
    public void updatePaymentTime(String orderId, LocalDateTime now) {
        log.info("开始更新支付时间");
        Thread.sleep(3000);
        int update = flBookorderMapper.updatePaymentTime(orderId,now);
        log.info("更新支付时间是否更新？"+update);
        log.info("更新支付时间完成");
    }

    @SneakyThrows
    @Async
    public void createPayInfo(String userId, String orderId) {
        log.info("开始创建支付信息");
        Thread.sleep(4000);
        //修改订单状态为已支付 并创建支付小票
        int update2 = flBookorderMapper.changeStatusByOrderId(orderId, 1);
        if(update2 == 1){
            String payId = IdUtil.getSnowflake().nextIdStr();
            String platformNumber = IdUtil.simpleUUID();
            String payplatform = "3";
            FlPayinfo payInfo = flPayinfoService.createPayInfo(userId, payId, orderId, payplatform, platformNumber);
            if(payInfo!=null)
            {
                log.warn("支付小票"+payInfo);
//                return Result.succ("支付成功！！",payInfo);
            }
        }

        log.info("创建支付信息完成");
    }

    /**
     * 累计消费金额
     * @param userId
     * @param bigDecimal
     */
    @SneakyThrows
    @Async
    public void addCumulativeRecharge(String userId, BigDecimal bigDecimal) {
        log.info("开始累加充值金额");
        Thread.sleep(2000);
        int update = flUserMapper.addCumulativeRecharge(userId,bigDecimal);
        log.info("累计充值金额是否更新？"+update);
        log.info("累加充值金额完成");
    }

    /**
     * 修改支付方式
     * @param orderId
     * @param i
     */
    @SneakyThrows
    @Async
    public void updatePaymentType(String orderId, int i) {
        log.info("开始修改支付方式");
        Thread.sleep(3000);
        int update = flBookorderMapper.updatePaymentTypeByOrderId(orderId,i);
        log.info("修改支付方式是否更新？"+update);
        log.info("修改支付方式完成");
    }
}
